home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Image;
-
- public class TicTacToe extends Applet {
- int white;
- int black;
- static final int[] moves = new int[]{4, 0, 2, 6, 8, 1, 3, 5, 7};
- static boolean[] won = (boolean[])(new byte[512]);
- static final int DONE = 511;
- // $FF: renamed from: OK int
- static final int field_0 = 0;
- static final int WIN = 1;
- static final int LOSE = 2;
- static final int STALEMATE = 3;
- boolean first = true;
- Image notImage;
- Image crossImage;
-
- static void isWon(int pos) {
- for(int i = 0; i < 511; ++i) {
- if ((i & pos) == pos) {
- won[i] = true;
- }
- }
-
- }
-
- int bestMove(int white, int black) {
- int bestmove = -1;
-
- label57:
- for(int i = 0; i < 9; ++i) {
- int mw = moves[i];
- if ((white & 1 << mw) == 0 && (black & 1 << mw) == 0) {
- int pw = white | 1 << mw;
- if (won[pw]) {
- return mw;
- }
-
- for(int mb = 0; mb < 9; ++mb) {
- if ((pw & 1 << mb) == 0 && (black & 1 << mb) == 0) {
- int pb = black | 1 << mb;
- if (won[pb]) {
- continue label57;
- }
- }
- }
-
- if (bestmove == -1) {
- bestmove = mw;
- }
- }
- }
-
- if (bestmove != -1) {
- return bestmove;
- } else {
- for(int i = 0; i < 9; ++i) {
- int mw = moves[i];
- if ((white & 1 << mw) == 0 && (black & 1 << mw) == 0) {
- return mw;
- }
- }
-
- return -1;
- }
- }
-
- boolean yourMove(int m) {
- if (m >= 0 && m <= 8) {
- if (((this.black | this.white) & 1 << m) != 0) {
- return false;
- } else {
- this.black |= 1 << m;
- return true;
- }
- } else {
- return false;
- }
- }
-
- boolean myMove() {
- if ((this.black | this.white) == 511) {
- return false;
- } else {
- int best = this.bestMove(this.white, this.black);
- this.white |= 1 << best;
- return true;
- }
- }
-
- int status() {
- if (won[this.white]) {
- return 1;
- } else if (won[this.black]) {
- return 2;
- } else {
- return (this.black | this.white) == 511 ? 3 : 0;
- }
- }
-
- public void init() {
- this.notImage = ((Applet)this).getImage(((Applet)this).getCodeBase(), "images/not.gif");
- this.crossImage = ((Applet)this).getImage(((Applet)this).getCodeBase(), "images/cross.gif");
- }
-
- public void paint(Graphics g) {
- Dimension d = ((Component)this).size();
- g.setColor(Color.black);
- int xoff = d.width / 3;
- int yoff = d.height / 3;
- g.drawLine(xoff, 0, xoff, d.height);
- g.drawLine(2 * xoff, 0, 2 * xoff, d.height);
- g.drawLine(0, yoff, d.width, yoff);
- g.drawLine(0, 2 * yoff, d.width, 2 * yoff);
- int i = 0;
-
- for(int r = 0; r < 3; ++r) {
- for(int c = 0; c < 3; ++i) {
- if ((this.white & 1 << i) != 0) {
- g.drawImage(this.notImage, c * xoff + 1, r * yoff + 1, this);
- } else if ((this.black & 1 << i) != 0) {
- g.drawImage(this.crossImage, c * xoff + 1, r * yoff + 1, this);
- }
-
- ++c;
- }
- }
-
- }
-
- public boolean mouseUp(Event evt, int x, int y) {
- switch (this.status()) {
- case 1:
- case 2:
- case 3:
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/return.au");
- this.white = this.black = 0;
- if (this.first) {
- this.white |= 1 << (int)(Math.random() * (double)9.0F);
- }
-
- this.first = !this.first;
- ((Component)this).repaint();
- return true;
- default:
- Dimension d = ((Component)this).size();
- int c = x * 3 / d.width;
- int r = y * 3 / d.height;
- if (this.yourMove(c + r * 3)) {
- ((Component)this).repaint();
- switch (this.status()) {
- case 1:
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
- break;
- case 2:
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
- case 3:
- break;
- default:
- if (this.myMove()) {
- ((Component)this).repaint();
- switch (this.status()) {
- case 1:
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
- break;
- case 2:
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/joy.au");
- case 3:
- break;
- default:
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/ding.au");
- }
- } else {
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/beep.au");
- }
- }
- } else {
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/beep.au");
- }
-
- return true;
- }
- }
-
- public String getAppletInfo() {
- return "TicTacToe by Arthur van Hoff";
- }
-
- static {
- isWon(7);
- isWon(56);
- isWon(448);
- isWon(73);
- isWon(146);
- isWon(292);
- isWon(273);
- isWon(84);
- }
- }
-